<?php
class Newcenter_model extends CI_Model
{
	public function __construct()
	{
		$this->load->library('session');
		$this->load->database();
	}
	public function getDetailState()
	{
		$sql = 'SELECT user_id FROM '.CI_Model::$detail.' WHERE user_id =?;';
		$result = $this->db->query($sql,array($_SESSION['id']))->row_array();
		return isset($result['user_id']) ? true : false;
	}

	public function getAuthState()
	{
		$sql = 'SELECT state FROM '.CI_Model::$state.' WHERE user_id  =?';
		$result = $this->db->query($sql,array($_SESSION['id']))->row_array();
		if(isset($result['state']))
		{
			return (int)$result['state'];
		}
		else
		{
			$sql = 'INSERT INTO '.CI_Model::$state.' (user_id,state) VALUES (?,?);';
			$this->db->query($sql,array($_SESSION['id'],0));
			return 0;
		}
	}

	public function updateAuthState($states)
	{
		$state = $this->getAuthState()+1;
		if($states > $state-1)
		{
			$state = $states;
		}
		$sql = 'UPDATE '.CI_Model::$state.' SET state = ? WHERE user_id = ?;';
		return $this->db->query($sql,array($state,$_SESSION['id']));
	}
	public function getDetailAll()
	{
		$sql = 'SELECT * FROM '.CI_Model::$detail.' WHERE user_id = ?;';
		$result = $this->db->query($sql,array($_SESSION['id']))->row_array();
		$sql = 'SELECT phone FROM '.CI_Model::$newUser.' WHERE auto_id = ? ;';
		$phone = $this->db->query($sql,array($_SESSION['id']))->row_array()['phone'];
		$result['phone'] = $phone;
		//var_dump($result);exit;
		return $result;
	}

	public function getPhone()
	{
		$sql = 'SELECT phone FROM '.CI_Model::$newUser.' WHERE auto_id = ? ;';
		$phone = $this->db->query($sql,array($_SESSION['id']))->row_array();
		return isset($phone['phone'])? $phone['phone'] : null;

	}

	public function updateDetail($data)
	{
		if($this->getDetailState())
		{
		    $sql = 'UPDATE '.CI_Model::$detail.' SET name = ?,school =?,sex =?,level =?,major = ?,school_num =?,school_num=?,nick=?,birth=?,homeland=?,emotion=?,sign=? WHERE user_id =?;';
		    $this->db->query($sql,array($data['name'],$data['school'],
				                                 $data['sex'],$data['xueli'],$data['level'],
				                                 $data['major'],$data['school_num'],$data['nick'],
				                                 $data['birth'],$data['homeland'],$data['emotion'],
				                                 $data['sign'],$_SESSION['id']));	
		}
		else
		{
			$sql = 'INSERT INTO '.CI_Model::$detail.' (user_id,name,school,sex,xueli,level,major,school_num,nick,birth,homeland,emotion,sign) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?);';
			$this->db->query($sql,array($_SESSION['id'],$data['name'],$data['school'],
				                                 $data['sex'],$data['xueli'],$data['level'],
				                                 $data['major'],$data['school_num'],$data['nick'],
				                                 $data['birth'],$data['homeland'],$data['emotion'],
				                                 $data['sign']));
		}
		
	}

}